﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.StaffPages.Sch
{
    /// <summary>
    /// ResultList 的摘要说明。
    /// </summary>
    public partial class ResultListPage : Page
    {
        private string bound;
        private int[] displayfieldsid;
        private string[] displayfieldsname;
        private string email;
        private string gender;

        //存放页眉文字
        private string[] headtext;
        private string mobile;
        private int positionid;
        private string staffname;

        protected override void OnInit(EventArgs e)
        {
            dgrd_StaffList.PageIndexChanged += dgrd_StaffList_PageIndexChanged;
            dgrd_StaffList.SortCommand += dgrd_StaffList_SortCommand;

            // The Reporting feature is temporarily disabled!
            // btn_Print.Click += btn_Print_Click;
            //
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                headtext = new String[dgrd_StaffList.Columns.Count];
                for (int i = 0; i < dgrd_StaffList.Columns.Count; i++)
                {
                    headtext[i] = dgrd_StaffList.Columns[i].HeaderText;
                }
                ViewState["headtext"] = headtext;

                //获得来源页面的句柄
                var searchform = (SearchPage) Context.Handler;
                staffname = searchform.StaffName.Trim();
                positionid = searchform.Postion;
                mobile = searchform.MobileSwitch ? searchform.Mobile : "";
                email = searchform.EmailSwitch ? searchform.Email : "";
                gender = searchform.GenderSwitch ? searchform.Gender : "";
                bound = searchform.SearchBound;
                displayfieldsid = searchform.SelectedFields;
                displayfieldsname = searchform.SelectedFieldsName;

                ViewState["staffname"] = staffname;
                ViewState["positionid"] = positionid.ToString();
                ViewState["mobile"] = mobile;
                ViewState["email"] = email;
                ViewState["gender"] = gender;
                ViewState["bound"] = bound;
                ViewState["displayfiledsid"] = displayfieldsid;

                Session["staffname"] = staffname;
                Session["positionid"] = positionid;
                Session["mobile"] = mobile;
                Session["email"] = email;
                Session["gender"] = gender;
                Session["bound"] = bound;
                Session["displayfieldsname"] = displayfieldsname;

                BindGrid(staffname, positionid, mobile, email, gender, displayfieldsid);
            }
            else
            {
                staffname = ViewState["staffname"].ToString();
                positionid = Int32.Parse(ViewState["positionid"].ToString());
                mobile = ViewState["mobile"].ToString();
                email = ViewState["email"].ToString();
                gender = ViewState["gender"].ToString();
                bound = ViewState["bound"].ToString();
                displayfieldsid = (int[]) ViewState["displayfiledsid"];

                //把页眉复位
                headtext = (string[]) ViewState["headtext"];
                for (int i = 0; i < dgrd_StaffList.Columns.Count; i++)
                {
                    dgrd_StaffList.Columns[i].HeaderText = headtext[i];
                }
            }

            base.OnLoad(e);
        }

        private void BindGrid(string staffname, int positionid, string mobile, string email, string gender, int[] displayfiledsid)
        {
            var staff = new Staff();
            DataTable dt;
            using (var dr = staff.Find(staffname, positionid, mobile, email, gender, bound))
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }
            if (ViewState["sortfield"] != null)
            {
                dt.DefaultView.Sort = ViewState["sortfield"] + " " + ViewState["sortdirect"];
            }

            dgrd_StaffList.DataSource = dt.DefaultView;

            //放入高速缓存中便于打印
            Cache["StaffList"] = dt;

            for (int i = 0; i < displayfieldsid.Length; i++)
            {
                dgrd_StaffList.Columns[displayfiledsid[i]].Visible = true;
            }
            dgrd_StaffList.DataBind();
        }

        private void dgrd_StaffList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            ((DataGrid) source).CurrentPageIndex = e.NewPageIndex;
            BindGrid(staffname, positionid, mobile, email, gender, displayfieldsid);
        }

        private void dgrd_StaffList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (ViewState["sortfield"] == null)
            {
                ViewState["sortfield"] = e.SortExpression;
                ViewState["sortdirect"] = "ASC";
            }
            else
            {
                if (ViewState["sortfield"].ToString() == e.SortExpression)
                {
                    ViewState["sortdirect"] = (ViewState["sortdirect"].ToString() == "ASC" ? "DESC" : "ASC");
                }
                else
                {
                    ViewState["sortfield"] = e.SortExpression;
                    ViewState["sortdirect"] = "ASC";
                }
            }

            foreach (DataGridColumn col in dgrd_StaffList.Columns)
            {
                if (col.SortExpression == ViewState["sortfield"].ToString())
                {
                    if (ViewState["sortdirect"].ToString() == "ASC")
                    {
                        col.HeaderText += "<img src='../../../images/asc.gif' border=0/>";
                    }
                    else
                    {
                        col.HeaderText += "<img src='../../../images/desc.gif' border=0/>";
                    }
                }
            }

            BindGrid(staffname, positionid, mobile, email, gender, displayfieldsid);
        }

#if false
        private void btn_Print_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.open('../Report/StaffReport.aspx','_blank')</script>");
        }
#endif
    }
}